package com.redoor.app.stake.basicdata.service.impl;

import com.redoor.app.stake.basicdata.model.RegistrationApplyForRedis;
import com.redoor.app.stake.basicdata.model.RegistrationForRedisEnum;
import com.redoor.app.stake.basicdata.service.RegistrationApplyRedisService;
import com.redoor.app.stake.common.utils.JsonUtils;
import com.redoornetwork.framework.data.redis.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RegistrationApplyForRedisImpl implements RegistrationApplyRedisService {
    @Autowired
    RedisUtils redisUtils;

    @Override
    public boolean addApplyInfoToRedis(Object object, RegistrationForRedisEnum registrationForRedisEnum) {
        if (null != object){
            return redisUtils.push(registrationForRedisEnum.getName(), JsonUtils.toJson(object)) > 0;
        }else{
            return false;
        }
    }

    @Override
    public List getApplyInfoFromRedis(int times, RegistrationForRedisEnum registrationForRedisEnum) {
        List applyInfoList = new ArrayList();
        if (times < 1){
            times =1 ;
        }
        if (null == registrationForRedisEnum){
            return applyInfoList;
        }
        while (applyInfoList.size() < times){
            Object applyInfo = getApplyInfoFromRedis(registrationForRedisEnum);
            if (null != applyInfo){
                applyInfoList.add(applyInfo);
            }else {
                break;
            }
        }
        return applyInfoList;
    }
    /**
     *  获取redis里面的注册码申请信息
     *    使用到的方法:
     *    com.redoor.app.stake.basicdata.service.impl.RegistrationApplyForRedisImpl#getApplyInfoFromRedis(int, com.redoor.app.stake.basicdata.model.RegistrationForRedisEnum)
     * @author ZhangMumu
     * @date 2018/4/3 0003 10:49
     * @param
     * @return
     */
    public Object getApplyInfoFromRedis(RegistrationForRedisEnum registrationForRedisEnum){
        if (null == registrationForRedisEnum){
            return null;
        }
        long size = redisUtils.lGetListSize(registrationForRedisEnum.getName());
        if (size > 0){
            return JsonUtils.readValue(redisUtils.pop(registrationForRedisEnum.getName()).toString(),RegistrationApplyForRedis.class);
        }
        return null;
    }
}
